package com.zheng.study.company.service.impl;

import com.zheng.study.base.bean.SystemConfig;
import com.zheng.study.base.dao.BaseDao;
import com.zheng.study.base.service.impl.BaseServiceImpl;
import com.zheng.study.company.dao.CompanyDao;
import com.zheng.study.company.entity.Company;
import com.zheng.study.company.entity.User;
import com.zheng.study.company.service.CompanyService;
import com.zheng.study.company.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * newFile
 * ============================================================================
 * author : dzw
 * createDate:  2018/8/21 。
 * ============================================================================
 */
@Service
public class CompanyServiceImpl extends BaseServiceImpl<Company, String> implements CompanyService {

	@Autowired
	private CompanyDao companyDao;
	@Autowired
	private UserService userService;
	@Autowired
	private SystemConfig systemConfig;

	public BaseDao<Company, String> getBaseDao() {
		return companyDao;
	}

	public Company createCompany(String name, User user) {
		Company company = new Company();
		company.setName(name);
		company.setOwner(user);
		save(company);

		user.setCompany(company);
		userService.update(user);
		return company;
	}

	@Override
	public String getCompanyLogoUrl(Company company) {
		if(company!=null && StringUtils.isNotEmpty(company.getLogoFileId())){
			return "../main/file/download?keyId=" + company.getLogoFileId();
		}

		if(StringUtils.isNotEmpty(systemConfig.getDefault_logo_url())){
			return systemConfig.getDefault_logo_url();
		}else{
			return "../resource/core/img/header/default_logo.jpg";
		}
	}
}
